package com.example.demo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Hsu琛君珩
 * @ClassName:UserController
 * @date 2023-11-30
 * @apiNote
 * @Version: v1.0
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        //校验参数合法性
        //if(username==null||username.length()==0||password==null||password.length()==0){
        //    return false;
        //}
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        //进行用户名密码校验
        if("admin".equals(userName)&&"admin".equals(password)){
            //设置session
            session.setAttribute("username","admin");
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request){
        //从session中获取登录用户
        HttpSession session=request.getSession(false);
        String userName=null;
        if(session!=null) userName= (String) session.getAttribute("username");
        return userName;
    }
}
